package com.tx.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tx.entity.LunBo;
import com.tx.entity.Page;
import com.tx.entity.User;
import com.tx.service.LunBoService;
import com.tx.utils.FileUtil;
import com.tx.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: tcx
 * @description:
 * @Date: 2019/11/14
 **/
@Controller("lunBoController")
@RequestMapping("/lunbo")
public class LunBoController {
    @Autowired
    private LunBoService lunBoService;
    /* *
     * @description:  打开管理轮播图界面
     * @author tcx
     * @date 2019/11/20
     * @param
     * @return java.lang.String
     */
    @RequestMapping("/teacher/tolunbo")
    public String toLunBo(Map<String,Object> map){
        //查询出所有轮播图信息
        map.put("listlunbo",lunBoService.getAll());
        return "/teacher_lunbo";
    }
    /* *
     * @description:  上传轮播图
     * @author tcx
     * @date 2019/11/25
     * @param file
     * @param session
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    @ResponseBody
    @RequestMapping(value = "/teacher/uploadlunbo", method = RequestMethod.POST)
    public Map<String,Object> uploadHead(@RequestParam("file") MultipartFile file, HttpSession session) throws IOException {

        //获取文件名
        String fileName = file.getOriginalFilename();
        Map map = new HashMap<String,Object>();
        String originalFileName = file.getOriginalFilename();
        //获取项目根路径
        String fileUrl = "/";
        fileUrl = session.getServletContext().getRealPath(fileUrl);
        //向url地址存储文件
        String url = FileUtil.uploadHeadImage(file, fileUrl,originalFileName);
        //从session中拿到用户id
        User user = (User) session.getAttribute("user");
        //存入轮播图上传用户中
        LunBo lunBo = new LunBo();
        lunBo.setlName(fileName);
        lunBo.setuId(user.getuId());//用户
        lunBo.setlPath(url);//路径
        lunBoService.add(lunBo);
        //返回json
        map.put("msg","ok");
        map.put("code",200);
        return map;
    }
    /* *
     * @description:  上传轮播图,返回图片路径
     * @author tcx
     * @date 2019/11/25
     * @param file
     * @param session
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    @ResponseBody
    @RequestMapping(value = "/teacher/uploadImg", method = RequestMethod.POST)
    public Map<String,Object> uploadImg(@RequestParam("file") MultipartFile file, HttpSession session) throws IOException {
        Map map = new HashMap<String,Object>();
        String originalFileName = file.getOriginalFilename();
        //获取项目根路径
        String fileUrl = "/";
        fileUrl = session.getServletContext().getRealPath(fileUrl);
        //向url地址存储文件
        String url = FileUtil.uploadHeadImage(file, fileUrl,originalFileName);
        //返回json
        map.put("msg","ok");
        map.put("code",200);
        map.put("lPath",url);
        return map;
    }




    @ResponseBody
    @RequestMapping(value = "/teacher/updatelunbo", method = RequestMethod.POST)
    public Map<String,Object> update(@RequestParam("file") MultipartFile file,HttpServletRequest request,HttpSession session) throws IOException {
        Map map = new HashMap<String,Object>();
        return map;
    }
    /* *
     * @description: 添加轮播图
     * @author tcx
     * @date 2019/11/21
     * @param lId
     * @return java.lang.String
     */
    @RequestMapping("/teacher/add")
    public String add(LunBo lunBo, HttpSession session, HttpServletRequest request){
        //从session里获得登录的用户
        User user = (User) session.getAttribute("user");
        //获取用户id存入数据库
        //存入轮播图上传用户中
        lunBo.setuId(user.getuId());//用户
        lunBoService.add(lunBo);
        return "redirect:/lunbo/teacher/tolunbo.do";
    }


    /* *
     * @description:  删除轮播图
     * @author tcx
     * @date 2019/11/21
     * @param lId
     * @return java.lang.String
     */
/*    @RequestMapping(value = "/teacher/remove",params = "lId")
    public String remove(Integer lId){
        lunBoService.remove(lId);
        return "redirect:/lunbo/teacher/tolunbo.do";
    }*/
    /* *
     * @description:  删除轮播图
     * @author tcx
     * @date 2019/11/25
     * @param request
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/teacher/remove")
    public String remove(HttpServletRequest request){
        Integer lId = Integer.parseInt(request.getParameter("lId"));
        Map<String, Object> map = new HashMap<String, Object>();
        if(lunBoService.remove(lId)>0){
            map.put("code", 200);
            map.put("msg","删除成功！");
        }else{
            map.put("code", -1);
            map.put("msg","删除失败！");
        }


        Gson gson = new Gson();
        return  gson.toJson(map);
    }
    /* *
     * @description:  修改轮播图
     * @author tcx
     * @date 2019/11/21
     * @param lunBo
     * @return java.lang.String
     */
    @RequestMapping("/teacher/update")
    public String update(LunBo lunBo){
        lunBoService.edit(lunBo);
        return "redirect:/lunbo/teacher/tolunbo.do";
    }
    /*================================================================================================================================Test===================================================================================================================*/

    @RequestMapping("/teacher/admin_list")
    @ResponseBody
    public String list(@RequestParam(value = "page", required = false) Integer currentPage,
                       @RequestParam(value = "limit", required = false) Integer rows,
                       @RequestParam(value = "keywords", required = false) String keywords){
        Page pageBean = new Page();
        pageBean.setCurrentPage(currentPage);//设置当前页
        pageBean.setEveryPage(rows);//设置每页显示数量
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", pageBean.getStart());//设置起始页
        map.put("size", pageBean.getEveryPage());//设置页面大小
        map.put("q", StringUtil.formatLike(keywords));List<LunBo> list = lunBoService.list(map);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm").create();
        Integer total = lunBoService.getTotal(map);//获得总数据
        map.clear();
        if(list!=null){
            map.put("data", list);
            map.put("count", total);
            map.put("code", 0);
            map.put("msg", "");
        }else{
            map.put("data", list);
            map.put("count", total);
            map.put("code", 1);
            map.put("msg", "");
        }
        return  gson.toJson(map);
    }

    /* *
     * @description:  删除用户
     * @author tcx
     * @date 2020/04/05
     * @param request
     * @return java.lang.String
     */
    @ResponseBody
    @RequestMapping("/teacher/admin_delete")
    public String delete(HttpServletRequest request){
        //Integer uId = Integer.parseInt(request.getParameter("uId"));
        String[] idsStr = request.getParameter("ids").split(",");
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < idsStr.length; i++) {
            lunBoService.remove(Integer.parseInt(idsStr[i]));
        }
        map.put("msg", 200);
        return  gson.toJson(map);
    }
}

